/*
 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "se_napi_oma.h"

#include "se_napi_oma_common.h"
#include "se_napi_oma_seservice.h"
#include "se_napi_oma_reader.h"
#include "se_napi_oma_session.h"
#include "se_napi_oma_channel.h"

namespace OHOS {
namespace SE {
namespace OMAPI {

static napi_value EnumConstructor(napi_env env, napi_callback_info info)
{
    DebugLog("Enter function EnumConstructor");
    napi_value thisArg = nullptr;
    void *data = nullptr;
    napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
    napi_value global = nullptr;
    napi_get_global(env, &global);
    return thisArg;
}

static napi_value RegisterEnumServiceState(napi_env env, napi_value exports)
{
    DebugLog("Enter function RegisterEnumServiceState");
    napi_value disConnected = nullptr;
    napi_value connected = nullptr;
    napi_create_int32(env, static_cast<int32_t>(ServiceState::DISCONNECTED), &disConnected);
    napi_create_int32(env, static_cast<int32_t>(ServiceState::CONNECTED), &connected);
    napi_property_descriptor desc[] = {
        DECLARE_NAPI_STATIC_PROPERTY("DISCONNECTED", disConnected),
        DECLARE_NAPI_STATIC_PROPERTY("CONNECTED", connected),
    };

    napi_value result = nullptr;
    napi_define_class(env, "ServiceState", NAPI_AUTO_LENGTH, EnumConstructor, nullptr,
        sizeof(desc) / sizeof(*desc), desc, &result);
    napi_set_named_property(env, exports, "ServiceState", result);
    return exports;
}

napi_value NapiSEOma::RegisterSEOmaFunc(napi_env env, napi_value exports)
{
    DebugLog("Enter function RegisterSEOmaFunc");
    RegisterEnumServiceState(env, exports);
    NapiSEOmaSEService::DefineSEOmaSEServiceJsClass(env, exports);
    NapiSEOmaReader::DefineSEOmaReaderJsClass(env, exports);
    NapiSEOmaSession::DefineSEOmaSessionJsClass(env, exports);
    NapiSEOmaChannel::DefineSEOmaChannelJsClass(env, exports);
    napi_property_descriptor desc[] = {
        DECLARE_NAPI_FUNCTION("newSEService", NapiSEOmaSEService::NewSEService),
        DECLARE_NAPI_FUNCTION("createService", NapiSEOmaSEService::CreateService),
        DECLARE_NAPI_FUNCTION("on", NapiSEOmaSEService::On),
        DECLARE_NAPI_FUNCTION("off", NapiSEOmaSEService::Off),
    };
    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
    return exports;
}

static napi_module seOMAModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = NapiSEOma::RegisterSEOmaFunc,
    .nm_modname = "secureElement",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterSEOMAModule(void)
{
    napi_module_register(&seOMAModule);
}
}  // namespace OMAPI
}  // namespace SE
}  // namespace OHOS
